package com.lucky.auth_boot.aop;

import com.lucky.auth_boot.common.Result;
import com.lucky.auth_boot.pojo.dto.LoginLogDTO;
import com.lucky.auth_boot.pojo.dto.UserDTO;
import com.lucky.auth_boot.pojo.dto.UserLoginDTO;
import com.lucky.auth_boot.pojo.po.LoginLogPO;
import com.lucky.auth_boot.pojo.po.UserPO;
import com.lucky.auth_boot.pojo.vo.LoginLogVO;
import com.lucky.auth_boot.service.LoginLogService;
import com.lucky.auth_boot.utils.RequestUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.tomcat.util.http.ResponseUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Date;

@Aspect
@Component
public class LoginLogAspect {
    @Resource
    private LoginLogService loginLogService;

    @Pointcut("execution(* com.lucky.auth_boot.controller.LoginController.postUserLogin(..))")
//    @Pointcut("execution(* com.lucky.auth_boot.service.impl.UserServiceImpl.queryUserLogin(..))")
    public void pointCut() {
    }

    @AfterReturning(value = "pointCut()",returning = "result")
    public void After(JoinPoint joinPoint, Object result) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        HttpServletRequest request = RequestUtil.getHttpServletRequest();
//        String url = request.getRequestURI();
//        System.out.println("请求URL:"+url);
//        System.out.println("来源IP:"+RequestUtil.getIp(request));
//        System.out.println("来源IP:"+request.getRemoteAddr());
//        Method method = signature.getMethod();
//        System.out.println("参数值集合："+ Arrays.asList(joinPoint.getArgs()));
        UserLoginDTO userLoginDTO = (UserLoginDTO) Arrays.asList(joinPoint.getArgs()).get(0);
//        System.out.println("注解方式AOP执行的方法 :" + method.getName() + " 执行完了");
//        获取返回数据
        Result rt = (Result) result;

        LoginLogDTO loginLogDTO = new LoginLogDTO();
        loginLogDTO.setUsername(userLoginDTO.getUsername());
        loginLogDTO.setIpaddr(RequestUtil.getIp(request));
        loginLogDTO.setAccessTime(new Date());
        if(rt.getCode() == 200) {
            loginLogDTO.setStatus(1);
            loginLogDTO.setMsg("登录成功");
        } else {
            loginLogDTO.setStatus(0);
            loginLogDTO.setMsg("登录失败");
        }
//        记录到数据库
        loginLogService.postLoginLog(loginLogDTO);
    }

    @AfterThrowing(value = " pointCut()",throwing = "t")
    public void afterThrowing(JoinPoint joinPoint, Throwable t) {
//        System.out.println("afterThrowing advice ..."+t.getMessage());
        HttpServletRequest request = RequestUtil.getHttpServletRequest();
        UserLoginDTO userLoginDTO = (UserLoginDTO) Arrays.asList(joinPoint.getArgs()).get(0);

        LoginLogDTO loginLogDTO = new LoginLogDTO();

        loginLogDTO.setUsername(userLoginDTO.getUsername());
        loginLogDTO.setIpaddr(RequestUtil.getIp(request));
        loginLogDTO.setAccessTime(new Date());
        loginLogDTO.setStatus(0);
        loginLogDTO.setMsg(t.getMessage());
//        记录到数据库
        loginLogService.postLoginLog(loginLogDTO);
    }
}
